package com.test.ServletUpload;

import com.test.BRBInference.BRBInference;
import com.test.jdbc.JDBCUtils2;
import com.test.readData.readData;
import com.test.readData.readModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UploadModel", value = "/UploadModel")
public class UploadModel extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String fileName = null;
        ArrayList<Double> uscale = new ArrayList<Double>();
        if (ServletFileUpload.isMultipartContent(request)) { //请求表单的格式
            FileItemFactory fileItemFactory = new DiskFileItemFactory();  //缓存区
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

            try {
                List<FileItem> list = servletFileUpload.parseRequest(request);  //表单列表

                    for(int i=0;i<5;++i) {
                        uscale.add(Double.parseDouble(list.get(i).getString()));
                    }

                        fileName = list.get(5).getString();


            } catch (FileUploadException e) {
                e.printStackTrace();
            }

            QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
            List<Object[]> modelList = null;
            try {
                modelList = queryRunner.query("select * from " + fileName, new ArrayListHandler());
            } catch (SQLException e) {
                e.printStackTrace();
            }

            ArrayList<Object[]> modelArray = new ArrayList<>();

            for (Object[] attr : modelList) {
                Object[] Temp = new Object[attr.length];
                for (int i=1;i<attr.length;i++) {
                    double temp = Double.parseDouble(attr[i].toString());
                    Temp[i] = BRBInference.single_round(temp);
                }
                Temp[0] = attr[0];
                modelArray.add(Temp);
            }


            ArrayList<ArrayList<Double>> modelArray2 = new ArrayList<ArrayList<Double>>();

            for (Object[] rule : modelList) {
                ArrayList<Double> model = new ArrayList<Double>();
                for (Object value : rule) {
                    double temp = Double.parseDouble(value.toString());
                    //System.out.println(temp);
                    model.add(temp);
                }
                model.remove(0);
                modelArray2.add(model);
            }


            HttpSession session = request.getSession();
            ArrayList<Object[]> dataList = (ArrayList<Object[]>) session.getAttribute("dataList");
            ArrayList<ArrayList<Double>> dataArray = new ArrayList<ArrayList<Double>>();

            for (Object[] attr : dataList) {
                ArrayList<Double> data = new ArrayList<Double>();
                for (Object value : attr) {
                    double temp = Double.parseDouble(value.toString());
                    //System.out.println(temp);
                    data.add(temp);
                }
                data.remove(0);
                dataArray.add(data);
            }

            ArrayList<Double> Actual_Result = new ArrayList<Double>();
            for(ArrayList<Double> attr :dataArray) {
                Actual_Result.add(attr.get(attr.size()-1));
            }

            BRBInference BRB = new BRBInference(uscale, dataArray, modelArray2);
            BRB.attr_identify();
            BRB.fuse();
            request.getSession().setAttribute("Inference_Result", BRB.getInference_Result());
            request.getSession().setAttribute("Actual_Result", Actual_Result);


            request.getSession().setAttribute("uscale", uscale);
            request.getSession().setAttribute("modelList", modelList);
            request.getSession().setAttribute("modelList2", modelArray);



            response.sendRedirect("/Java_web/ModelShow.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
